package com.guigu.qingcheng.orderservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.orderservice.mapper.TbOrderItemMapper;
import com.guigu.qingcheng.orderservice.mapper.TbOrderLogMapper;
import com.guigu.qingcheng.orderservice.mapper.TbOrderMapper;
import com.guigu.qingcheng.orderservice.service.TbOrderItemService;
import com.guigu.qingcheng.pojo.order.TbOrder;
import com.guigu.qingcheng.pojo.order.TbOrderItem;
import com.guigu.qingcheng.pojo.order.TbOrderLog;
import com.guigu.qingcheng.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbOrderItemServiceImpl extends ServiceImpl<TbOrderItemMapper, TbOrderItem> implements TbOrderItemService {
    @Resource
    private TbOrderMapper mapper;
    @Resource
    private TbOrderLogMapper tbOrderLogMapper;

    @Override
    public R findPage(Integer page, Integer size, Map<String, Object> searchMap) {
        Page<TbOrderItem> orderItemPage = new Page<>(page,size);

        QueryWrapper<TbOrderItem> wrapper = new QueryWrapper<>();
        if (searchMap != null) {
            if (searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wrapper.eq("id",searchMap.get("id"));
            if (searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wrapper.like("name",searchMap.get("name"));
        }
        baseMapper.selectPage(orderItemPage,wrapper);
        return R.ok().data("total",orderItemPage.getTotal()).data("rows",orderItemPage.getRecords());
    }

    @Override
    public List<TbOrderItem> findByOrderId(String id) {
        QueryWrapper<TbOrderItem> wrapper =new QueryWrapper<>();
        wrapper.eq("order_id",id);
        List<TbOrderItem> tbOrderItems = baseMapper.selectList(wrapper);
        return tbOrderItems;
    }

    @Override
    public List<Map> findByOrderIdlj(String id) {
        return baseMapper.list(id);
    }

    @Override
    public R updates(List<TbOrderItem> tbOrderItems, String id) {
        String[] split = id.split(",");
        for (TbOrderItem tbOrderItem: tbOrderItems) {
            for (String ids: split) {
                TbOrder order = mapper.selectById(tbOrderItem.getOrderId());
                tbOrderItem.setMoney(tbOrderItem.getPayMoney()-(Integer.parseInt(ids)*tbOrderItem.getPrice()));
                tbOrderItem.setNum(tbOrderItem.getNum() - Integer.parseInt(ids));
                baseMapper.updateById(tbOrderItem);
                tbOrderItem.setId(null);
                tbOrderItem.setMoney(Integer.parseInt(ids)*tbOrderItem.getPrice());
                tbOrderItem.setNum(Integer.parseInt(ids));
                baseMapper.insert(tbOrderItem);
                TbOrderLog log = new TbOrderLog();
                log.setOperater("system");
                log.setOperateTime(new Date());
                log.setOrderStatus(order.getOrderStatus());
                log.setPayStatus(order.getPayStatus());
                log.setConsignStatus(order.getConsignStatus());
                log.setRemarks("拆分订单");
                log.setOrderId(tbOrderItem.getOrderId());
                tbOrderLogMapper.insert(log);
            }
        }
        return R.ok();
    }

}
